package com.vadmin.vadminjava.service;

import com.alibaba.fastjson.JSONObject;
import com.vadmin.vadminjava.common.BigDecimalUtils;
import com.vadmin.vadminjava.common.LogEnum;
import com.vadmin.vadminjava.common.MoneyLogEnum;
import com.vadmin.vadminjava.common.ResultSetToJavaBean;
import com.vadmin.vadminjava.db.DBUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.vadmin.vadminjava.common.TooUtil.isEmpty;


@Component
public class FinanceService  extends CommonExtends{

    org.slf4j.Logger logger = LoggerFactory.getLogger(WxCouponCashService.class.getName());

    @Autowired
    WxUserMoneyService wxUserMoneyService;
    @Autowired
    AdminLogService adminLogService;

    /**
     * 用户资金明细
     * @param params
     * @return
     */
    public JSONObject userMoneyLog(JSONObject params){
        String table  = "wxwork_user_money_log";//数据库表
        String conditions = "";//条件

        //参数处理
        //String  transactionMinimum = params.getString("transactionMinimum");
        //if(StringUtils.isNoneEmpty(transactionMinimum)){
        //    conditions += " AND transaction_minimum >= '" + transactionMinimum + "' ";
        //}

        return commonDataListFlexible(table,conditions,params);
    }

    /**
     * 员工资金结算处理
     * 2022-03-31 测试
     * @param params
     * @return
     */
    public JSONObject userMoneySettle (JSONObject params){
        //初始化
        DBUtil dbUtil = new DBUtil();
        JSONObject map = new JSONObject();
        map.put("resultFlag",false);
        map.put("resultMsg","操作失败");

        String log = "";//日志

        //参数处理
        String _userId = params.getString("userId");
        String _amountD = params.getString("amount");
        String _act = params.getString("act");
        String _remark = params.getString("remark");

        if(isEmpty(_amountD)){ map.put("","缺少amount参数");return map;}
        if(isEmpty(_act)){ map.put("","缺少act参数");return map;}

        //处理数值，主要是可能有小数
        Double _amount = Double.valueOf(_amountD);


        //处理类型数字，用于sql写入
        String _type = "";
        Integer _typeCode = 0;

        if(_act.equals("increase")){
            _type     = MoneyLogEnum.TYPE_ADD.name;
            _typeCode = MoneyLogEnum.TYPE_ADD.code;

        }else if(_act.equals("subtract")){
            _type     = MoneyLogEnum.TYPE_REDUCE.name;
            _typeCode = MoneyLogEnum.TYPE_REDUCE.code;
            _amount = _amount * - 1;//如果是减的，则把数值变成负数

        }

        try {
            dbUtil.conn.setAutoCommit(false);

            //找到员工的资金数据，进行计算
            String sql2 = "select * from wxwork_user where user_id = '" + _userId + "'";
            Double _money = ResultSetToJavaBean.copyJsonSingleQuery(dbUtil,sql2).getDouble("money");
            String _moneyAfter = BigDecimalUtils.add(_money,_amount).toString();

            //在这个位置更新员工资金
            wxUserMoneyService.userMoneyChange(dbUtil,_userId,_amount.toString());//写入

            //记录明细
            JSONObject logParams = new JSONObject();
            logParams.put("user_id",_userId);
            logParams.put("external_userid","");
            logParams.put("open_id","");
            logParams.put("money_before",_money);
            logParams.put("money",_amount);
            logParams.put("money_after",_moneyAfter);
            logParams.put("coupon_id","");//
            logParams.put("coupon_code","");
            logParams.put("event",MoneyLogEnum.ADMIN_SETTLE.name);
            logParams.put("event_code",MoneyLogEnum.ADMIN_SETTLE.code);
            logParams.put("type",_type);
            logParams.put("type_code",_typeCode);
            logParams.put("remark",_remark);
            wxUserMoneyService.userMoneyLog(dbUtil,logParams);//写入

            dbUtil.conn.commit();

            map.put("resultFlag",true);
            map.put("resultMsg","操作成功");

            log = "成功进行了员工" + _userId + "的奖金操作，最终操作数值是" + _amount;
            logger.info(log);
            adminLogService.write(LogEnum.WORKER_MONEY.name,LogEnum.WORKER_MONEY.index,log);

        } catch (Exception ex) {
            log = "在进行员工" + _userId + "的奖金操作，最终操作数值是" + _amount + "但是出错了。。。";
            logger.error(log,ex);
            adminLogService.write(LogEnum.WORKER_MONEY.name,LogEnum.WORKER_MONEY.index,log);
            map.put("resultFlag",true);
            map.put("resultMsg","操作失败了");

            try {
                dbUtil.conn.rollback();
            } catch (Exception e) {

            }
        } finally {
            dbUtil.closeNoErr();
        }


        return map;
    }

}
